package com.hk.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hk.exception.BaseException;
import com.hk.mapper.ActivityMapper;
import com.hk.model.pojo.Activity;
import com.hk.model.vo.ActivityVo;
import com.hk.model.vo.Params;
import com.hk.service.ActivityService;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.service.Response;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 获取活动列表
     *
     * @param pageNum
     * @param pageSize
     * @param code
     * @param beginCreateTime
     * @param endCreateTime
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public ActivityVo getActivityList(Integer pageNum, Integer pageSize, Integer code, Data beginCreateTime, Data endCreateTime, Data beginTime, Data endTime) {
        //分页配置
        PageHelper.startPage(pageNum, pageSize);
        //通过条件查询所有的表

        List<Activity> list = activityMapper.getActivityList(code, beginCreateTime, endCreateTime, beginTime, endTime);
        Params params = new Params();
        params.setTotal(list.size());


        return new ActivityVo(list.size(), list, 200, "成功", params);
    }

    /**
     * 新增活动数据
     *
     * @param activity
     */
    @Override
    public void addActivity(Activity activity) {
//生成的随机8为数
        StringBuilder code = new StringBuilder();
        Random random = new Random();
        String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        for (int i = 0; i < 8; i++) {
            int index = random.nextInt(characters.length());
            code.append(characters.charAt(index));

        }
        String codeString = code.toString();
        activity.setCode(codeString);
        //   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //接收到的时间也要进行转换为日期
//        String beginTime = activity.getBeginTime();
//        String endTime = activity.getEndTime();
//        try {
//            Date beginDate = sdf.parse(beginTime);
//            Date endDate = sdf.parse(endTime);
//            activity.setBeginTime(sdf.format(beginDate));
//            activity.setEndTime(sdf.format(endDate));
//        } catch (ParseException e) {
//            throw new BaseException("解析异常");
//        }

        activityMapper.insert(activity);
    }

    @ApiOperation(value = "删除活动")
    @Override
    public void deleteById(Integer id) {
        super.removeById(id);
    }

    @ApiOperation(value = "获取活动id")
    @Override
    public Activity getId(Integer id) {
      return   activityMapper.getId(id);
    }

    @ApiOperation(value = "修改活动信息")
    @Override
    public void updateActivity(Activity activity) {
        activityMapper.updateActivity(activity);
    }
}
